package org.hypnus.e.engine;

import java.io.File;

import org.hypnus.e.engine.config.ServiceConfigManager;
import org.hypnus.e.engine.message.EngineContext;
import org.hypnus.e.engine.registry.JdkRegistry;
import org.hypnus.e.engine.registry.RegistryBroker;
import org.hypnus.e.engine.service.Service;

public class DefaultServiceEngine implements ServiceEngine, LifeCycle {

	private String sCfgPath;

	private RegistryBroker registryBroker ;

	private Service service;
	
	public RegistryBroker getRegistryBroker() {
		return registryBroker;
	}

	public void setRegistryBroker(RegistryBroker registryBroker) {
		this.registryBroker = registryBroker;
	}

	public void setsCfgPath(String sCfgPath) {
		this.sCfgPath = sCfgPath;
	}


	public void setService(Service service) {
		this.service = service;
	}

	public Service getService() {
		return service;
	}

	public String getsCfgPath() {
		return sCfgPath;
	}


	@Override
	public void consume(EngineContext sCxt) {
		service.run(sCxt);
	}

	@Override
	public void startup() {
		ServiceConfigManager configManager = ServiceConfigManager.getInstance();
		String cfgPath = this.getClass().getClassLoader().getResource("").getPath()+getsCfgPath() ;
		configManager.setCfgPath(cfgPath) ;
		configManager.parseConfig();
		RegistryBroker broker = RegistryBroker.getInstance();
		broker.setRegistry(new JdkRegistry()) ;
		broker.setRegistryPath(this.getClass().getClassLoader().getResource("").getPath()+"");
		broker.init();
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub

	}
	
	public static void main(String[] args) {
		File file = new File(DefaultServiceEngine.class.getClassLoader().getResource("").getPath());
//		file
		File[] arrFile = file.listFiles() ;
		for (File file2 : arrFile) {
			if(file2.isFile() && file2.getName().indexOf("service")>-1){
				System.out.println(file2.getName());
			}
		}
		System.out.println(DefaultServiceEngine.class.getClassLoader().getResource("").toString());
		System.out.println(file.isDirectory() );
	}

}
